﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace myomscl_web.Utils
{
    public partial class ucShiftControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            if (lbSource.SelectedItem != null)
            {
                var li = lbSource.SelectedItem;
                li.Selected = false;
                lbSelected.Items.Add(li);
                lbSource.Items.Remove(li);
            }
        }

        protected void btnUnselect_Click(object sender, EventArgs e)
        {
            if (lbSelected.SelectedItem != null)
            {
                var li = lbSelected.SelectedItem;
                li.Selected = false;
                lbSource.Items.Add(li);
                lbSelected.Items.Remove(li);
            }
        }

        public void LoadSources(List<ListItem> source, List<ListItem> selected)
        {
            lbSource.Items.Clear();
            lbSource.Items.AddRange(source.ToArray());

            lbSelected.Items.Clear();
            lbSelected.Items.AddRange(selected.ToArray());
        }

        public void LoadAll(List<ListItem> source)
        {
            lbSource.Items.Clear();
            lbSource.Items.AddRange(source.ToArray());
        }

        public void LoadSelected(List<ListItem> selected)
        {
            lbSelected.Items.Clear();
            lbSelected.Items.AddRange(selected.ToArray());
        }

        public ListItemCollection GetSelected()
        {
            return lbSelected.Items;
        }

        protected void btnUp_Click(object sender, EventArgs e)
        {
            if (lbSelected.SelectedItem != null && lbSelected.SelectedIndex > 0)
            {
                var li = lbSelected.SelectedItem;
                int ind = lbSelected.SelectedIndex;
                lbSelected.Items.Remove(li);
                lbSelected.Items.Insert(--ind, li);
                
            }
        }

        protected void btnDown_Click(object sender, EventArgs e)
        {
            if (lbSelected.SelectedItem != null && lbSelected.SelectedIndex <lbSelected.Items.Count-1)
            {
                var li = lbSelected.SelectedItem;
                int ind = lbSelected.SelectedIndex;
                lbSelected.Items.Remove(li);
                lbSelected.Items.Insert(++ind, li);
            }

        }

    }
}